Skip to content

Show source on GitHub

Super simple header node.

Example: Regular

Jinja

{{ "Header with level 2" | MkHeader(level=2) }}

Python

MkHeader('Header with level 2')

Header with level 2

## Header with level 2
<h2 id="header-with-level-2">Header with level 2</h2>
*******************
Header with level 2
*******************

Example: Header levels

Jinja

{% for i in range(1, 7) %}

{{ ("Header level " ~ i) | MkHeader(level=i) }}

{% endfor %}

Python

MkContainer(
    [
        MkHeader("Header level 1", level=1),
        MkHeader("Header level 2"),
        MkHeader("Header level 3", level=3),
        MkHeader("Header level 4", level=4),
        MkHeader("Header level 5", level=5),
        MkHeader("Header level 6", level=6),
    ]
)

Header level 1

Header level 2

Header level 3

Header level 4

Header level 5
Header level 6
# Header level 1

## Header level 2

### Header level 3

#### Header level 4

##### Header level 5

###### Header level 6
<h1 id="header-level-1">Header level 1</h1>
<h2 id="header-level-2">Header level 2</h2>
<h3 id="header-level-3">Header level 3</h3>
<h4 id="header-level-4">Header level 4</h4>
<h5 id="header-level-5">Header level 5</h5>
<h6 id="header-level-6">Header level 6</h6>
MkContainer
├── MkHeader('Header level 1', level=1)
├── MkHeader('Header level 2')
├── MkHeader('Header level 3', level=3)
├── MkHeader('Header level 4', level=4)
├── MkHeader('Header level 5', level=5)
╰── MkHeader('Header level 6', level=6)

Bases: MkNode

__init__

__init__(
    text: str | MkNode | None = "",
    *,
    level: int = 2,
    exclude_from_search: bool = False,
    **kwargs: Any
)

Parameters:

Name Type Description Default
text str | MkNode | None

Header text

''
level int

Header level

2
exclude_from_search bool

Whether section should be included in search.

False
kwargs Any

Keyword arguments passed to parent

{}
Name Children Inherits
MkNode
mknodes.basenodes.mknode
Base class for everything which can be expressed as Markup.
graph TD
  94721311200512["mkheader.MkHeader"]
  94721308848336["mknode.MkNode"]
  94721311766592["node.Node"]
  140564252373184["builtins.object"]
  94721308848336 --> 94721311200512
  94721311766592 --> 94721308848336
  140564252373184 --> 94721311766592
/home/runner/work/mknodes/mknodes/mknodes/basenodes/mkheader/metadata.toml
[metadata]
icon = "mdi:format-header-pound"
name = "MkHeader"
group = "base"

[examples.regular]
title = "Regular"
jinja = """
{{ "Header with level 2" | MkHeader(level=2) }}
"""

[examples.header_levels]
title = "Header levels"
jinja = """
{% for i in range(1, 7) %}

{{ ("Header level " ~ i) | MkHeader(level=i) }}

{% endfor %}
"""

[output.markdown]
template = """
{{ "#" * node.level }} {{ node.text }}{% if node.exclude_from_search %} { data-search-exclude }{% endif %}
"""

# [output.html]
# template = """
# <h{{ node.level }}>{{ node.text }}</h{{ node.level }}>
# """

[output.rst]
template = """
{% set line = {1: '#' , 2: '*' , 3: '=' , 4: '-' , 5: '^' , 6: '"' }[node.level] * (node.text | length) %}
{{ line }}
{{ node.text }}
{{ line }}
"""
mknodes.basenodes.mkheader.MkHeader
class MkHeader(mknode.MkNode):
    """Super simple header node."""

    ICON = "material/format-header-pound"
    STATUS = "new"

    def __init__(
        self,
        text: str | mknode.MkNode | None = "",
        *,
        level: int = 2,
        exclude_from_search: bool = False,
        **kwargs: Any,
    ):
        """Constructor.

        Args:
            text: Header text
            level: Header level
            exclude_from_search: Whether section should be included in search.
            kwargs: Keyword arguments passed to parent
        """
        super().__init__(**kwargs)
        self.text = str(text or "")
        self.level = level
        self.exclude_from_search = exclude_from_search

    def _to_markdown(self) -> str:
        level_str = "#" * self.level
        suffix = " { data-search-exclude }" if self.exclude_from_search else ""
        return f"{level_str} {self.text}{suffix}"